package com.apes.pi.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.pi.model.PiConnector;
import com.apes.pi.model.PiConnectorParams;
import com.apes.pi.parser.QueueCacheManager;
import com.apes.pi.repository.PiConnectorParamsRepository;
import com.apes.pi.repository.PiConnectorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 连接器服务类
 *
 * @author wuq
 * @create 2019-10-22 15:38
 */
@Service
public class ConnectorService extends DomainService {

    @Autowired
    PiConnectorRepository piConnectorRepository;

    @Autowired
    PiConnectorParamsRepository piConnectorParamsRepository;

    public PiConnector create(SimpleRequest request){
        PiConnector connector = request.getO(PiConnector.class);
        for (PiConnectorParams item : connector.getParams()) {
            item.setConnector(connector);
        }
        connector = piConnectorRepository.saveAndFlush(connector);
        SpringManager.getBean(QueueCacheManager.class).addPiConnector(connector);
        return connector;
    }

    public PiConnector update(SimpleRequest request){
        PiConnector connector = request.getO(PiConnector.class);
        for (PiConnectorParams item : connector.getParams()) {
            item.setConnector(connector);
        }
        connector = piConnectorRepository.saveAndFlush(connector);
        SpringManager.getBean(QueueCacheManager.class).addPiConnector(connector);
        return connector;
    }

    public String delete(SimpleRequest request){
        PiConnector connector = request.getO(PiConnector.class);
        piConnectorRepository.delete(connector);
        SpringManager.getBean(QueueCacheManager.class).removePiConnector(connector.getId());
        return "删除成功";
    }
}
